package zhangzhifan;

import java.awt.*;

import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
/**
 * 
 * ����ѡ��Ի���
 * @author ��֪��
 * 
 * 
 */
public class FontChooseDlg extends JDialog{
	
	//�Ի������
	private JLabel lblChooseFont = new JLabel("Choose Font");
	private JLabel lblChooseSize = new JLabel("Enter Size");
	private JLabel lblNewLabel = new JLabel("Choose Style");
	private JList<String> FontList = new JList<String>();
	private JList<String> StyleList = new JList<String>();
	private final JTextArea usedField;
	private final JScrollPane scrollPane = new JScrollPane();
	private final JButton btnOK = new JButton("\u786E\u5B9A");
	private final JButton btnCancel = new JButton("\u53D6\u6D88");
	private final JButton btnUseSetting = new JButton("\u5E94\u7528");
	private final JTextField sizeField = new JTextField();
	private final JButton btnColorChoose = new JButton("\u989C\u8272");
	//�Ի������?���ڴ���
	final JDialog f;
	//Ĭ�������С����ɫ
	int FontSize = 12;
	Color FontColor;
	Color originalColor;
	//��ʼ����
	Font originalFont;

	
	/**
	 * ��ʼ�������б�������б�
	 */
	void InitLists() {
		//��ȡϵͳ�����б�
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        String[] fontList = ge.getAvailableFontFamilyNames();
        scrollPane.setBounds(10, 36, 140, 193);
        scrollPane.setViewportView(FontList);
        FontList.setListData(fontList);
        String[] Style = {"Plain", "Bold", "Italic", "Bold&Italic"};
        StyleList.setBounds(160, 37, 76, 134);
        StyleList.setListData(Style);
	}
	
	public FontChooseDlg(JTextArea textField) {
		f = this;
		setSize(new Dimension(342, 278));
		setResizable(false);
		usedField = textField;
		originalFont = usedField.getFont();
		originalColor = FontColor = usedField.getForeground();
			//����
			getContentPane().setLayout(null);
			lblChooseFont.setBounds(10, 11, 85, 15);
			getContentPane().add(lblChooseFont);
			lblChooseSize.setBounds(160, 181, 85, 15);
			getContentPane().add(lblChooseSize);
			lblNewLabel.setBounds(160, 11, 85, 15);
			getContentPane().add(lblNewLabel);
			getContentPane().add(StyleList);
			getContentPane().add(scrollPane);
			btnUseSetting.setBounds(244, 67, 76, 23);
			getContentPane().add(btnUseSetting);
			btnOK.setBounds(244, 100, 76, 23);
			getContentPane().add(btnOK);
			sizeField.setBounds(160, 206, 76, 23);
			sizeField.setText("12");
			getContentPane().add(sizeField);
			sizeField.setColumns(10);
			btnColorChoose.setBounds(244, 34, 76, 23);
			getContentPane().add(btnColorChoose);
			
			btnCancel.setBounds(244, 133, 76, 23);
			getContentPane().add(btnCancel);
			
		//"Ӧ��"��ť����Ӧ
		btnUseSetting.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				usedField.setFont(getFont());
				usedField.setForeground(FontColor);
			}
		});
		//"ȷ��"��ť����Ӧ
		btnOK.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Close();
			}
		});
		//�����С�ĸ���
		sizeField.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				FontSize = Integer.parseInt(sizeField.getText());
			}
		});
		//"��ɫ"��ť����Ӧ
		btnColorChoose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Color color = JColorChooser.showDialog(f, "ColorChooser", Color.black);
				if(color != null) {
					FontColor = color;
					usedField.setForeground(FontColor);
				}
			}
		});
		//"ȡ��"��ť����Ӧ
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				usedField.setFont(originalFont);
				usedField.setForeground(originalColor);
				Close();
			}
		});
		InitLists();
		}
	/**
	 * �ر�
	 */
	private void Close() {
		this.setVisible(false);
	}
	/**
	 * ��ݸ���ѡ���������
	 */
	public Font getFont() {
		Font res =  new Font(FontList.getSelectedValue(), StyleList.getSelectedIndex(), FontSize);
		return res;
	}
}
